package utils.connect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectSqlDaoImpl implements ConnectSqlDao {
    //该对象为了连接数据库，方便后面的servlet等等对于数据库的操作
    private Connection connection = null;

    public Connection getConnection() {
        return connection;
    }

    //为了安全起见，使用单例模式得到对象
    private static class SingletonConnectSqlDaoImpl{
        private static final ConnectSqlDaoImpl INSTANT = new ConnectSqlDaoImpl();
    }

    public ConnectSqlDaoImpl() {
    }

    public static final ConnectSqlDaoImpl getInStance(){
        return SingletonConnectSqlDaoImpl.INSTANT;
    }
    @Override
    public void initConnect() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");//驱动名字
        connection  = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/equipmentManager?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC",
                "root","199771.");//得到数据库来连接
        System.out.println("success");
    }

    @Override
    public void closeConnection() throws SQLException {
        connection.close();
    }
}
